from django.contrib import admin
from django.contrib.auth.models import Permission as DjangoPermission, Group as DjangoGroup
from .models import PermissionCategory, PermissionExtension, RoleExtension, UserRoleAssignment


@admin.register(PermissionCategory)
class PermissionCategoryAdmin(admin.ModelAdmin):
    """权限分类管理"""
    
    list_display = ['name', 'code', 'sort_order', 'created_at']
    list_filter = ['created_at']
    search_fields = ['name', 'code', 'description']
    ordering = ['sort_order', 'name']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'code', 'description')
        }),
        ('显示设置', {
            'fields': ('sort_order',)
        }),
    )


@admin.register(PermissionExtension)
class PermissionExtensionAdmin(admin.ModelAdmin):
    """权限扩展管理"""
    
    list_display = ['permission_name', 'module', 'category', 'status', 'sort_order', 'created_at']
    list_filter = ['module', 'category', 'status', 'created_at']
    search_fields = ['permission__name', 'permission__codename', 'module', 'description']
    ordering = ['sort_order', 'module', 'permission__name']
    
    fieldsets = (
        ('关联信息', {
            'fields': ('permission', 'category')
        }),
        ('扩展信息', {
            'fields': ('module', 'resource_path', 'frontend_route', 'api_endpoint')
        }),
        ('描述信息', {
            'fields': ('description', 'status')
        }),
        ('显示设置', {
            'fields': ('sort_order',)
        }),
    )
    
    def permission_name(self, obj):
        return obj.permission.name
    permission_name.short_description = '权限名称'
    permission_name.admin_order_field = 'permission__name'


@admin.register(RoleExtension)
class RoleExtensionAdmin(admin.ModelAdmin):
    """角色扩展管理"""
    
    list_display = ['group_name', 'code', 'level', 'status', 'is_system', 'sort_order', 'created_at']
    list_filter = ['status', 'is_system', 'level', 'created_at']
    search_fields = ['group__name', 'code', 'description']
    ordering = ['sort_order', 'level', 'group__name']
    
    fieldsets = (
        ('关联信息', {
            'fields': ('group',)
        }),
        ('扩展信息', {
            'fields': ('code', 'description', 'level')
        }),
        ('状态设置', {
            'fields': ('status', 'is_system')
        }),
        ('显示设置', {
            'fields': ('sort_order',)
        }),
    )
    
    def group_name(self, obj):
        return obj.group.name
    group_name.short_description = '角色名称'
    group_name.admin_order_field = 'group__name'


@admin.register(UserRoleAssignment)
class UserRoleAssignmentAdmin(admin.ModelAdmin):
    """用户角色分配管理"""
    
    list_display = ['user_username', 'role_name', 'status', 'assigned_at', 'expires_at', 'assigned_by_username']
    list_filter = ['status', 'assigned_at', 'expires_at', 'role__group__name']
    search_fields = ['user__username', 'user__email', 'role__group__name', 'reason']
    ordering = ['-assigned_at']
    date_hierarchy = 'assigned_at'
    
    fieldsets = (
        ('分配信息', {
            'fields': ('user', 'role', 'status')
        }),
        ('时间设置', {
            'fields': ('expires_at',)
        }),
        ('分配记录', {
            'fields': ('assigned_by', 'reason')
        }),
    )
    
    readonly_fields = ['assigned_at', 'created_at', 'updated_at']
    
    def user_username(self, obj):
        return obj.user.username
    user_username.short_description = '用户名'
    user_username.admin_order_field = 'user__username'
    
    def role_name(self, obj):
        return obj.role.name
    role_name.short_description = '角色名称'
    role_name.admin_order_field = 'role__group__name'
    
    def assigned_by_username(self, obj):
        return obj.assigned_by.username if obj.assigned_by else '-'
    assigned_by_username.short_description = '分配人'
    assigned_by_username.admin_order_field = 'assigned_by__username'
    
    def save_model(self, request, obj, form, change):
        if not change:  # 新建时
            obj.assigned_by = request.user
        super().save_model(request, obj, form, change)


# 注意：Django内建的Permission和Group模型已经有默认的Admin配置
# 如果需要自定义，可以在这里重新注册，但需要小心处理已注册的情况
